<?php

require_once(PASTA_BANCO_DAOGENERICO . "DaoGenerico.class.php");
require_once(PASTA_CONEXAO . "Conexao.class.php");
require_once(PASTA_SMARTY . "Inicia.class.php");

class DaoGenerico {
    /*
     * Funcão para inserir no banco
     * $array["tabela"] e $array["id"]
     * devem ser informados no formulário do TPL no input text display:none;
     */

    public function insert($array = null) {
        $result = 0;
        $conexao = new Conexao();
        try {
            $sql = "SELECT * FROM  " . $array["tabela"] . "  WHERE " . $array["id"] . " = -1";
            unset($array["tabela"]);
            unset($array["id"]);
            unset($array["nome_unico"]);

            $con = $conexao->getConexao();
            $rs = $con->Execute($sql);

            $insertSQL = $con->GetInsertSQL($rs, $array);
            $con->Execute($insertSQL);
            $result = $con->Insert_ID();
        } catch (Exception $e) {
            $con->Close();
            $result = 0;
            $objInicia = new Inicia();
            $objInicia->imprimeTela(CABECALHO, PASTA_CONTEUDO_MODEL . "tagException.tpl", RODAPE, INDEX, $e->getMessage());
            die();
        }
        $con->Close();
        return $result;
    }

    public function update($array = null) {

        $result = true;
        $con = new Conexao();
        try {
            $sql = "SELECT * FROM  " . $array["tabela"] . "  WHERE " . $array["id"] . " = " . $array["idReg"];
            unset($array["tabela"]);
            unset($array["url"]);
            unset($array["id"]);
            unset($array["nome_unico"]);
            $con = Conexao::getConexao();
            $rs = $con->Execute($sql);
            $updateSQL = $con->GetUpdateSQL($rs, $array);
            if (empty($updateSQL)) {
                $updateSQL = $con->GetUpdateSQL($rs, $array, 1);
            }
            $con->Execute($updateSQL);
        } catch (Exception $e) {
            $con->Close();
            $result = false;
            $objInicia = new Inicia();
            $objInicia->imprimeTela(CABECALHO, PASTA_CONTEUDO_MODEL . "tagException.tpl", RODAPE, INDEX, $e->getMessage());
            die();
        }
        $con->Close();
        return $result;
    }

}

?>